package com.android.ailaoshan.picture;

import android.os.Environment;

import com.android.ailaoshan.common.ResourceInfo;
import com.android.ailaoshan.location.LastLocation;

import org.litepal.LitePal;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * 图片进一步修改
 */
class UpdatePicture {

    private final String PIC_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
            + File.separator +"AiLaoShan"+ File.separator + "Picture" + File.separator;

    /**
     * 删除图片
     */
    void deletePicture(String picName){
        new Thread(
                () -> {
                    // 删除文件
                    File file = new File(PIC_PATH+picName);
                    if(file.exists()){
                        file.delete();
                    }
                    // 删除记录
                    LitePal.deleteAll(ResourceInfo.class,"name = ?",file.getName());
                }
        ).start();
    }

    /**
     * 批量删除图片
     */
    void deleteAllPicture(ArrayList<String> pictures){
        new Thread(
           () -> {
               for( String picture : pictures ){
                   String filePath = PIC_PATH + picture;
                   File file = new File(filePath);
                   if(file.exists()){
                       file.delete();
                   }
                   // 删除记录
                   LitePal.deleteAll(ResourceInfo.class,"name = ?",file.getName());
               }
           }
        ).start();
    }

    /**
     * 保存图片
     * @param saveType 保存类型
     *                 1 保存新数据
     *                 2 更新原始数据
     */
    void savePictureInfo(ArrayList<String> names,int protect,String description,int saveType){
        new Thread(
           () -> {
               if(saveType == 1){
                   // 所属批次
                   long fileNo = System.currentTimeMillis();
                   // 记录时间
                   long date = System.currentTimeMillis();
                   // 经纬度
                   HashMap<String,Double> geoInfo = LastLocation.getLocation();

                   // 保存图片
                   for( int i=0; i<names.size(); i++){
                       ResourceInfo resourceInfo = new ResourceInfo();
                       resourceInfo.setName(names.get(i));
                       resourceInfo.setDate(date);
                       resourceInfo.setProtect(protect);

                       if(geoInfo != null){
                           resourceInfo.setLat(geoInfo.get("lat"));
                           resourceInfo.setLng(geoInfo.get("lng"));
                       }
                       resourceInfo.setFileNo(fileNo);
                       resourceInfo.setDescribe(description);
                       resourceInfo.setFileType(1);
                       resourceInfo.setUpload(2);

                       // 保存信息
                       resourceInfo.save();
                   }
               }else{
                   List<ResourceInfo> resourceInfoList = LitePal
                           .where("name=?",names.get(0))
                           .find(ResourceInfo.class);
                   if(resourceInfoList.size() > 0){
                       ResourceInfo resourceInfo = resourceInfoList.get(0);
                       long fileNo = resourceInfo.getFileNo();
                       String deccribe = resourceInfo.getDescribe();
                       double lat = resourceInfo.getLat();
                       double lng = resourceInfo.getLng();
                       int xProtect = resourceInfo.getProtect();
                       long xDate = resourceInfo.getDate();

                       LitePal.deleteAll(ResourceInfo.class,"fileNo = ?",fileNo +"");

                       for(String name : names){
                           ResourceInfo uResourceInfo = new ResourceInfo();

                           uResourceInfo.setName(name);
                           uResourceInfo.setUpload(2);
                           uResourceInfo.setLng(lng);
                           uResourceInfo.setLat(lat);
                           uResourceInfo.setFileType(1);
                           uResourceInfo.setFileNo(fileNo);
                           uResourceInfo.setDescribe(deccribe);
                           uResourceInfo.setProtect(xProtect);
                           uResourceInfo.setDate(xDate);

                           uResourceInfo.save();
                       }
                   }else{
                       savePictureInfo(names,protect,description,1);
                   }
               }
           }
        ).start();
    }
}
